package com.zf.tjlis;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.ecbt.util.Md5;

public class LoginFrame extends BaseScale {

	private static final long serialVersionUID = 1L;
	
	public LoginFrame() {
		JFrame jf=new JFrame("登录 MES3.0 系统");    //创建一个JFrame对象
        jf.setBounds(400, 300, 350, 300);    //设置窗口大小和位置
        
        JPanel all = new JPanel();
        
		JPanel up = new JPanel();    //用户名Panel
		JLabel jl = new JLabel("登录名"); // 创建一个标签
		up.add(jl);
		JTextField user = new JTextField(20);
		up.add(user);
		
		JPanel passp = new JPanel();    //用户名Panel
		JLabel passlab = new JLabel("密     码"); // 创建一个标签
		passp.add(passlab);
		JPasswordField password = new JPasswordField(20);
		passp.add(password);
		
		JPanel butp = new JPanel();    //用户名Panel
		JButton login=new JButton("登录");
		JButton cancel=new JButton("取消");
		butp.add(login);
		butp.add(cancel);
		
		JLabel message = new JLabel(""); // 消息框
		message.setForeground(Color.red);
		
		
		login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(user.getText()==null || user.getText().trim().equals("")) {
					message.setText("用户名不能为空。");
					return;
				}
				if(password.getPassword()==null || String.valueOf(password.getPassword()).equals("")) {
					message.setText("密码不能为空。");
					return;
				}
				Statement stmt;
				try {
					String strpass = Md5.getMD5String(String.valueOf(password.getPassword()));
					stmt = conn.createStatement();
					ResultSet rset = stmt.executeQuery("select s.loginname from sys_user s where s.loginname='"+user.getText()+"' and s.pwd='"+strpass+"'");
					if(rset.next()) {
						message.setText("登录成功。");
						new ElectronicScale();
						jf.setVisible(false);
					}else {
						message.setText("登录失败。");
					}
					rset.close();
					stmt.close();
				} catch (Exception e1) {
					message.setText("登录异常："+e1.getMessage());
					return;
				}
			}
		});
		
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		message.setSize(350, 100);
		all.add(up, BorderLayout.NORTH);
		all.add(passp, BorderLayout.CENTER);
		all.add(butp, BorderLayout.SOUTH);
		all.add(message, BorderLayout.SOUTH);
		
		jf.add(all);
		
		jf.setVisible(true);    //设置窗口可见
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] agrs) {
		new LoginFrame(); // 创建一个实例化对象
	}
}
